package com.xuchen.zhenyang.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.xuchen.zhenyang.base.BaseQuery;
import com.xuchen.zhenyang.base.Result;
import com.xuchen.zhenyang.controller.base.BaseController;
import com.xuchen.zhenyang.core.annotation.RequestLog;
import com.xuchen.zhenyang.entity.Payment;
import com.xuchen.zhenyang.entity.base.MyEntityWrapper;
import com.xuchen.zhenyang.enums.PaymentTypeEnum;
import com.xuchen.zhenyang.service.PaymentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("payment")
@RequiresPermissions("payment")
public class PaymentController extends BaseController {


    @Autowired
    PaymentService paymentService;

    @GetMapping("")
    String index(HttpServletRequest request) {
        setAttributeEnums(request);
        return "payment/payment-list";
    }

    @GetMapping("list")
    @ResponseBody
    Result list(BaseQuery baseQuery, Payment myEntity, String params) {
        if (StrUtil.isNotBlank(params)) {
            myEntity = JSONObject.parseObject(params).toJavaObject(Payment.class);
        }
        MyEntityWrapper<Payment> wrapper = new MyEntityWrapper<>(baseQuery, myEntity);
        wrapper.eq("payment_type").eq("user_id").timeBetween("pay_time");
        List<Payment> list = paymentService.selectList(wrapper);
        return Result.success(PageHelper.freeTotal(), list);
    }

    @GetMapping("toAdd")
    String toAdd(HttpServletRequest request) {
        setAttributeEnums(request);
        return "payment/payment-add";
    }

    @PostMapping("doAdd")
    @ResponseBody
    @RequestLog
    Result doAdd(Payment myEntity) {
        myEntity.setCreateUser(getSessionUserName());
        paymentService.insert(myEntity);
        return Result.success();
    }

    @GetMapping("toEdit")
    String toEdit(Payment myEntity, HttpServletRequest request) {
        setAttributeEnums(request);
        request.setAttribute("myEntity", paymentService.selectById(myEntity));
        return "payment/payment-edit";
    }

    @PostMapping("doEdit")
    @ResponseBody
    @RequestLog
    Result doEdit(Payment myEntity) {
        paymentService.updateById(myEntity);
        return Result.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @RequestLog
    Result delete(Payment myEntity) {
        paymentService.deleteById(myEntity);
        return Result.success();
    }

    private void setAttributeEnums(HttpServletRequest request) {
        request.setAttribute("paymentType", PaymentTypeEnum.getMap());
    }
}
